﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SBBitmap;

namespace ImageControl
{
    public partial class VorschauBox : UserControl
    {
        public delegate void PictureBoxDeleteHandler(object sender, EventArgs e);
        public delegate void PictureBoxClickHandler(object sender, ImageEventArgs e);
        public delegate void HistogramEventHandler(object sender, HistogramEventArgs e);
        public event PictureBoxDeleteHandler OnDeleting;
        public event EventHandler OnDeletingEverything;
        public new event PictureBoxClickHandler OnClick;
        public event HistogramEventHandler OnAddHistogram;

        public bool Subtract
        {
            get
            {
                return checkBoxSubtract.Checked;
            }
        }

        public Bitmap Image
        {
            get
            {
                return pictureBox.Image as Bitmap;
            }
            set
            {
                pictureBox.Image = value;
            }
        }

        public VorschauBox(Bitmap image)
        {
            InitializeComponent();
            BackColor = SystemColors.GradientActiveCaption;
            Image = image;

            WarpBitmap bmp = new WarpBitmap(Image.Clone() as Bitmap);
            int r = 0, g = 0, b = 0;
            int tmpRed = 0, tmpGreen = 0, tmpBlue = 0;
            double[] histRed = new double[256];
            double[] histGreen = new double[256];
            double[] histBlue = new double[256];
            int pixel = 0;
            for (int j = 0; j < bmp.Height; j++)
            {
                for (int i = 0; i < bmp.Width; i++)
                {
                    pixel++;
                    tmpRed = bmp.GetPixel(i, j).R;
                    tmpGreen = bmp.GetPixel(i, j).G;
                    tmpBlue = bmp.GetPixel(i, j).B;

                    histRed[tmpRed]+= 1.0;
                    histGreen[tmpGreen] += 1.0;
                    histBlue[tmpBlue] += 1.0;

                    r += tmpRed;
                    g += tmpGreen;
                    b += tmpBlue;
                }
            }

            r /= pixel;
            g /= pixel;
            b /= pixel;
            AverageColor = Color.FromArgb(r, g, b);

            HistRedValues = histRed;
            HistGreenValues = histGreen;
            HistBlueValues = histBlue;
        }

        public String Label
        {
            get
            {
                return labelName.Text;
            }
            set
            {
                labelName.Text = value;
            }
        }

        public Color AverageColor
        {
            get;
            set;
        }

        public double[] HistRedValues
        {
            get;
            set;
        }

        public double[] HistGreenValues
        {
            get;
            set;
        }

        public double[] HistBlueValues
        {
            get;
            set;
        }



        private void löschenToolStripMenuItem_Click(object sender, EventArgs e)
        {
         OnDeleting(this,  new EventArgs()); 
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            BackColor = SystemColors.ButtonShadow;
            OnClick(this, new ImageEventArgs { Width = pictureBox.Image.Width, Height = pictureBox.Image.Height, RGB = AverageColor });
        }

        private void alleLöschenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OnDeletingEverything(this, new EventArgs());
        }

        private void histogrammHinzufügenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OnAddHistogram(this, new HistogramEventArgs{Red = HistRedValues, Green = HistGreenValues, Blue = HistBlueValues, Label = Label});
        }
    }

    public class ImageEventArgs : EventArgs
    {
        public int Width { get; set; }
        public int Height { get; set; }
        public Color RGB { get; set; }
    }

    public class HistogramEventArgs : EventArgs
    {
        public double[] Red { get; set; }
        public double[] Green { get; set; }
        public double[] Blue { get; set; }
        public String Label { get; set; }
    }
}
